#!/usr/bin/python 
#-*- Coding: UTF-8 -*-

import socket
import struct
import sys
import time
import SocketServer
from  threading import *
import binascii
from array import *
from datetime import datetime
#from event import event


def do_udp_connection( server_ip,server_port):
    udpsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udpsock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    #self.sock.settimeout(30)
    print "UDP Bind:",local_addr,udpsock.bind(local_addr)
    udpsock.sendto("Hello server!",(server_ip,server_port))
    udpsock.close()


name = "Test_0001"
rendez_addr = ("192.168.1.1",5543)
local_ip = "192.168.0.1"
local_port = 5544
local_addr = ("0.0.0.0",local_port)

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
print "Bind:",local_addr,sock.bind(local_addr)

sock.connect(rendez_addr)
print "Publish:"
sock.send("PUBLISH %s %s %s"%( name, local_ip, local_port))

do_udp_connection( rendez_addr[0], rendez_addr[1])

ack = sock.recv(1024)
print ack
sock.close()
